﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using NDEngineering.Utilities;
using NDEngineering.Common;

namespace NDEngineering.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(_Default).Name);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSendEmail_Click(object sender, EventArgs e)
        {
            try
            {


                if (!String.IsNullOrEmpty(txtName.Text) || !String.IsNullOrEmpty(txtEmail.Text) || !String.IsNullOrEmpty(txtContent.Text))
                {
                    string host = Utils.GetAppSettingByKey(Constants.SMTP_HOST);
                    int port = int.Parse(Utils.GetAppSettingByKey(Constants.SMTP_PORT));
                    string from = Utils.GetAppSettingByKey(Constants.SMTP_USERNAME);
                    string password = Utils.GetAppSettingByKey(Constants.SMTP_PASSWORD);
                    bool enableSSL = false;
                    if (Utils.GetAppSettingByKey(Constants.SMTP_SSL) == "true")
                    {
                        enableSSL = true;
                    }
                    string displayName = "Solid Designs's Support";
                    string to = Utils.GetAppSettingByKey(Constants.EMAIL_CONTACT);
                    string subject = "a new contact from Solid Designs website";
                    string content = String.Format(@"Hello,<br/>There is a new contact on Solid Designs website with information:<br/><br/>
                                            <b>Email:</b> {0}<br/>
                                            <b>Name:</b> {1}<br/>
                                            <b>Content:</b> {2}<br/><br/><br/>
                                    Regards,<br/><br/>
                                    Solid Designs<br/>
                                    10678 Jones Rd., Houston, TX<br/>
                                    Phone: 281-890-3333 <br/>
                                    Fax: 281-890-3337  <br/>
                                    Cell: 832-453-9999   <br/>
                                ", txtEmail.Text, txtName.Text, txtContent.Text.Replace("\n", "<br/>"));
                    Utils.SendEmail(host, port, from, password, enableSSL, displayName, from, to, subject, content);
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), "alert('Thank you! we received your message.');", true);
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), "alert('Please input your information in Contact form!!!'); document.getElementById('contact').focus();", true);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }
    }
}